package mi.payment.constants;

import java.util.Map;

import mi.payment.utils.iso8583.MsgField;
import sunmi.payment.L3.L3Params;
import sunmi.sunmiui.utils.LogUtil;
/**
 * @author Lee64 on 2017/3/9.
 */

public class ParseUtils {

    /**
     * 添加自定义的商户号
     */
    public static Map<String, MsgField> addCustomTransID(Map<String, MsgField> map) {
        L3Params l3Params = Params.sL3Params;
        if (l3Params != null) {
            String transId = l3Params.transId;
            MsgField f047 = map.get("F047");
            if (f047 != null && transId != null && transId.length() > 0) {
                String dataStr = f047.getDataStr();
                dataStr += "|OI=" + transId;
                f047.setDataStr(dataStr);
                f047.setDataLength(dataStr.length());
            }
        }
        return map;
    }

    /**
     * 解析44域
     */
    public static CardMechanism parseF044(byte[] dataBytes) {
        try {
            if (dataBytes.length == 88) {

                CardMechanism card = new CardMechanism();

                byte[] data = new byte[24];
                System.arraycopy(dataBytes, 0, data, 0, 24);
                String issuer = TTLVUtils.decodeGBkHexByte(data);
                LogUtil.d("nsz", "发卡机构：" + issuer);
                card.issuer = issuer;

                data = new byte[24];
                System.arraycopy(dataBytes, 24, data, 0, 24);
                String receive = TTLVUtils.decodeGBkHexByte(data);
                LogUtil.d("nsz", "收单机构：" + receive);
                card.receive = receive;

                data = new byte[40];
                System.arraycopy(dataBytes, 48, data, 0, 40);
                String msg = TTLVUtils.decodeGBkHexByte(data);
                LogUtil.d("nsz", "应答消息：" + msg);
                card.msg = msg;

                return card;
            }

            if (dataBytes.length == 96) {
                CardMechanism card = new CardMechanism();

                byte[] data = new byte[24];
                System.arraycopy(dataBytes, 0, data, 0, 24);
                String issuer = TTLVUtils.decodeGBkHexByte(data);
                LogUtil.d("nsz", "发卡机构：" + issuer);
                card.issuer = issuer;

                data = new byte[24];
                System.arraycopy(dataBytes, 24, data, 0, 24);
                String receive = TTLVUtils.decodeGBkHexByte(data);
                LogUtil.d("nsz", "收单机构：" + receive);
                card.receive = receive;

                data = new byte[8];
                System.arraycopy(dataBytes, 48, data, 0, 8);
                String type = TTLVUtils.decodeGBkHexByte(data);
                LogUtil.d("nsz", "卡类型：" + type);
                card.type = type;

                data = new byte[40];
                System.arraycopy(dataBytes, 56, data, 0, 40);
                String msg = TTLVUtils.decodeGBkHexByte(data);
                LogUtil.d("nsz", "应答消息：" + msg);
                card.msg = msg;

                return card;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }


}
